import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by Shine Kaye on 2016/10/18.
 */
public class CutImageUtils {

    /**
     * Cut the left half of the image
     *
     * @param srcImage: Path of the source image
     * @param resultImage: Path of the after-cut image
     */
    public static void cutImage(String srcImage, String resultImage, float k1, float k2) {

        try {
            BufferedImage image = ImageIO.read(new File(srcImage));
            int height = image.getHeight();
            int width = image.getWidth();
            int[] imageArray = new int[height*width];
            image.getRGB(0, 0, width, height, imageArray, 0, width);

            int tarHeight = (int)(height*k1);
            int tarWidth = (int)(width*k2);
            BufferedImage newImage = new BufferedImage(tarWidth, tarHeight, BufferedImage.TYPE_INT_RGB);
            newImage.setRGB(0, 0, tarWidth, tarHeight, imageArray, 0, width);
            String fileName = (new File(srcImage.trim())).getName();
            File file = new File(resultImage + "/afterCut" + fileName);
            ImageIO.write(newImage, "jpg", file);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
